package me.baicai.weixin.identity.service.impl;

import me.baicai.weixin.identity.repository.*;
import me.baicai.weixin.identity.service.ManageService;
import me.baicai.weixin.application.BasicService;
import me.baicai.weixin.application.Result;
import me.baicai.weixin.conf.IdentityKey;
import me.baicai.weixin.entity.config.ShopMenu;
import me.baicai.weixin.entity.gcb.PageEntity;
import me.baicai.weixin.entity.identity.Employee;
import me.baicai.weixin.entity.identity.Identity;
import me.baicai.weixin.entity.identity.Shop;
import me.baicai.weixin.provider.repository.FaultRecordRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;


@Service
public class ManageServiceImpl extends BasicService implements ManageService
{
    @Autowired
    private ShopRepository shopRepository;

    @Autowired
    private LoginRepository loginRepository;

    @Autowired
    private DeviceRepository deviceRepository;

    @Autowired
    private FaultRecordRepository faultRecordRepository;

    @Autowired
    private EmployeeRepository employeeRepository;

    @Autowired
    private MoneyLogRepository moneyLogRepository;

    @Autowired
    private ShopMenuRepository shopMenuRepository;

    @Autowired
    private AccountRepository accountRepository;

    @Override
    public Result manageShop(String openid, Integer page, String input)
    {
        Identity identity = loginRepository.findByOpenid(openid);
        if (identity.getType().equalsIgnoreCase(IdentityKey.customer) ||
            identity.getType().equalsIgnoreCase(IdentityKey.shop))
        {
            return this.error(401, "无权限进入此页");
        }
        Pageable pageable = new PageEntity(page, 10, Sort.Direction.DESC, "id");
        if (identity.getType().equalsIgnoreCase(IdentityKey.engineer))
        {
            if (null != identity && !"".equalsIgnoreCase(input))
                return this.success(
                    shopRepository.ManageShopByEngineerInput(openid, "%" + input + "%", pageable));
            return this.success(shopRepository.ManageShopByEngineer(openid, pageable));
        }
        else if (identity.getType().equalsIgnoreCase(IdentityKey.salesman))
        {
            if (null != identity && !"".equalsIgnoreCase(input))
                return this.success(
                    shopRepository.ManageShopBySalesmanInput(openid, "%" + input + "%", pageable));
            return this.success(shopRepository.ManageShopBySalesman(openid, pageable));
        }
        else
        {
            if (null != identity && !"".equalsIgnoreCase(input))
                return this.success(
                    shopRepository.ManageShopByAgentInput(openid, "%" + input + "%", pageable));
            return this.success(shopRepository.ManageShopByAgent(openid, pageable));
        }
    }

    @Override
    public Result manageDevice(String openid, Integer page, String input)
    {
        Pageable pageable = new PageEntity(page, 10, Sort.Direction.DESC, "id");
        if (null != input && !"".equalsIgnoreCase(input))
        {
            return this.success(
                deviceRepository.getDeviceManage(openid, "%" + input + "%", pageable));
        }
        return this.success(deviceRepository.getDeviceManage(openid, pageable));
    }

    @Override
    public Result manageRecord(String openid, Integer page, String beginDate, String endDete,
                               Integer status, String type)
    {
        Pageable pageable = new PageEntity(page, 10, Sort.Direction.DESC, "id");
        if (type.equalsIgnoreCase("admin"))
            return this.success(deviceRepository.getRecordByStatusOK(status, beginDate, endDete, pageable));
        else
            return this.success(
                deviceRepository.getRecordByStatus(openid, status, beginDate, endDete, pageable));
    }

    @Override
    public Result verifyDevce(String sn)
    {
        return this.success(deviceRepository.existsBySn(sn));
    }

    @Override
    public Result repairDevce(Integer id, Integer status)
    {
        return this.success(deviceRepository.repairDevce(id, status));
    }

    @Override
    public Result shopList(String openid)
    {
        return this.success(shopRepository.findAllByEngIdOrSalesmanId(openid, openid));
    }

    @Override
    public Result submitShop(String openid, String shopName, String tel, String address,
                             Integer commission, Integer id, String name, String latitude,
                             String longitude)
    {
        if (null == id)
            id = 0;
        Employee employee = employeeRepository.findEmployeeByOpenid(openid);
        Identity identity = loginRepository.getIdentityById(id);
        if (null != identity && !identity.getType().equalsIgnoreCase(IdentityKey.customer))
        {
            return error("只限普通用户申请, 该对象已经为 : " + identity.getType());
        }

        Shop shop = null;
        if (null == employee)
        {
            if (null != identity)
            {
                shop = new Shop(identity.getOpenid(), shopName, address, tel, commission,
                    openid, latitude, longitude);
            }
            else
            {
                shop = new Shop("", shopName, address, tel, commission, openid, latitude,
                    longitude);
            }
        }
        else
        {
            if (null != identity)
            {
                shop = new Shop(identity.getOpenid(), shopName, address, tel, commission,
                    employee.getAgentId(), latitude,
                    longitude);
            }
            else
            {
                shop = new Shop("", shopName, address, tel, commission, employee.getAgentId(),
                    latitude,
                    longitude);
            }
        }

        shopRepository.saveAndFlush(shop);
        ShopMenu shopMenu = new ShopMenu();
        shopMenu.setShopId(shop.getId());
        shopMenu.setGkey(1);
        shopMenu.setIsuse(1);
        shopMenu.setPrice(2F);
        shopMenu.setTime(1);
        shopMenuRepository.saveAndFlush(shopMenu);
        shopMenu = new ShopMenu();
        shopMenu.setShopId(shop.getId());
        shopMenu.setGkey(2);
        shopMenu.setIsuse(1);
        shopMenu.setPrice(3F);
        shopMenu.setTime(2);
        shopMenuRepository.saveAndFlush(shopMenu);
        shopMenu = new ShopMenu();
        shopMenu.setShopId(shop.getId());
        shopMenu.setGkey(3);
        shopMenu.setIsuse(1);
        shopMenu.setPrice(4F);
        shopMenu.setTime(12);
        shopMenuRepository.saveAndFlush(shopMenu);
        shopMenu = new ShopMenu();
        shopMenu.setShopId(shop.getId());
        shopMenu.setGkey(4);
        shopMenu.setIsuse(1);
        shopMenu.setPrice(5F);
        shopMenu.setTime(24);
        shopMenuRepository.saveAndFlush(shopMenu);
        return this.success(shop);
    }

    @Override
    public Result incomeStatistics(String openid, String beginDate, String endDate)
    {
        Identity identity = loginRepository.findByOpenid(openid);
        return this.success(shopRepository.incomeStatistics(openid, beginDate, endDate,
            identity.getType()));
    }

    @Override
    public Result orderStatistics(String openid, String date)
    {
        return this.success(shopRepository.orderStatistics(openid, date,
            loginRepository.findByOpenid(openid).getType()));
    }

    @Override
    public Result faultRecord(String openid, Integer id, String type)
    {
        return this.success(faultRecordRepository.findAllByShopIdAndStatus(id, type));
    }

    @Override
    public Result incomeStatistics(String openid, Integer page)
    {
        Pageable pageable = new PageEntity(page, 10, Sort.Direction.DESC, "created");
        Map map = new HashMap();
        map.put("orderCount", moneyLogRepository.getOrderCountByEmp(openid));
        map.put("amount", accountRepository.findAccountByOpenid(openid).getCumulative());
        map.put("deviceCount", moneyLogRepository.getDeviceCountByEmp(openid));
        map.put("order", moneyLogRepository.getOrderByEmp(openid, pageable));
        return this.success(map);
    }
}
